<?php
declare (strict_types = 1);

namespace app\api\listener\invite;


use app\common\model\activity\InviteBind as InviteBindModel;
use think\facade\Log;


class BindHandle
{
    /**
     * 邀请活动 事件监听处理
     *
     * @return mixed
     */
    public function handle($event)
    {
        try {
            if($event['inviter_id'] == 0) return true;
            if($event['inviter_id'] == $event['invitee_id']) return true;
            $condition = [
                ['inviter_id','=',$event['inviter_id']],
                ['invitee_id','=',$event['invitee_id']],
            ];
            $rel = (new InviteBindModel())->where($condition)->lock(true)->find();
            if(!$rel){
                $rel = (new InviteBindModel())::create([
                    'inviter_id' => $event['inviter_id'],
                    'invitee_id' => $event['invitee_id']
                ]);
                if(!$rel->id) return false;
            }
            return true;
        }catch (\Exception $exception){
            Log::info('邀请绑定失败:'.$exception->getMessage());
            return false;
        }

    }
}
